/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.httpclient5.httpclient;

/**
 * httpclient5配置
 * 
 * @author tomcat
 * @version 1.0
 */
public final class HttpClient5Config {

    /**
     * 环境变量定义
     */
    private static final String ENV_MAX_IDLE_TIME = "HTTP_MAX_IDLE_TIME";
    private static final String ENV_CONNECTION_TIMEOUT = "HTTP_CONNECTION_TIMEOUT";
    private static final String ENV_RESPONSE_TIMEOUT = "HTTP_RESPONSE_TIMEOUT";
    private static final String ENV_SOCKET_TIMEOUT = "HTTP_SOCKET_TIMEOUT";
    private static final String ENV_SOCKET_RECEIVE_BUFFER_SIZE = "HTTP_SOCKET_RECEIVE_BUFFER_SIZE";
    private static final String ENV_SOCKET_SEND_BUFFER_SIZE = "HTTP_SOCKET_SEND_BUFFER_SIZE";

    /**
     * 最大空闲时间
     */
    private int maxIdleTime = 60000;
    /**
     * 获取连接超时时间
     */
    private int connectionTimeout = 5 * 1000;
    /**
     * 请求响应超时时间
     */
    private int responseTimeout = 60 * 1000;
    /**
     * TCP连接超时时间
     */
    private int socketTimeout = 15 * 1000;
    /**
     * TCP接收缓冲区大小
     */
    private int rcvBufSize = 8 * 1024;
    /**
     * TCP发送缓冲区大小
     */
    private int sndBufSize = 8 * 1024;

    /**
     * 构造函数
     */
    private HttpClient5Config() {
        // 从环境变量中获取配置
        this.maxIdleTime = Integer.parseInt(System.getenv().getOrDefault(ENV_MAX_IDLE_TIME, Integer.toString(maxIdleTime)));
        this.connectionTimeout = Integer.parseInt(System.getenv().getOrDefault(ENV_CONNECTION_TIMEOUT, Integer.toString(connectionTimeout)));
        this.responseTimeout = Integer.parseInt(System.getenv().getOrDefault(ENV_RESPONSE_TIMEOUT, Integer.toString(responseTimeout)));
        this.socketTimeout = Integer.parseInt(System.getenv().getOrDefault(ENV_SOCKET_TIMEOUT, Integer.toString(socketTimeout)));
        this.rcvBufSize = Integer.parseInt(System.getenv().getOrDefault(ENV_SOCKET_RECEIVE_BUFFER_SIZE, Integer.toString(rcvBufSize)));
        this.sndBufSize = Integer.parseInt(System.getenv().getOrDefault(ENV_SOCKET_SEND_BUFFER_SIZE, Integer.toString(sndBufSize)));
    }

    public static HttpClient5Config createDefault() {
        return new HttpClient5Config();
    }

    public HttpClient5Config maxIdleTime(int maxIdleTime) {
        this.maxIdleTime = maxIdleTime;
        return this;
    }

    public HttpClient5Config connectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
        return this;
    }

    public HttpClient5Config responseTimeout(int responseTimeout) {
        this.responseTimeout = responseTimeout;
        return this;
    }

    public HttpClient5Config socketTimeout(int socketTimeout) {
        this.socketTimeout = socketTimeout;
        return this;
    }

    public HttpClient5Config rcvBufSize(int rcvBufSize) {
        this.rcvBufSize = rcvBufSize;
        return this;
    }

    public HttpClient5Config sndBufSize(int sndBufSize) {
        this.sndBufSize = sndBufSize;
        return this;
    }

    public int getMaxIdleTime() {
        return maxIdleTime;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public int getResponseTimeout() {
        return responseTimeout;
    }

    public int getSocketTimeout() {
        return socketTimeout;
    }

    public int getRcvBufSize() {
        return rcvBufSize;
    }

    public int getSndBufSize() {
        return sndBufSize;
    }
}
